package com.yy.xshell.process.resultHandler;

import com.yy.xshell.core.ShellContext;
import com.yy.xshell.core.SshCore;
import com.yy.xshell.auth.SessionContext;
import com.yy.xshell.auth.UserSessionInfo;
import com.yy.xshell.process.Handler;
import org.apache.commons.lang.StringUtils;

/**
 * @author wxy
 * @version 1.0
 * @date 2024/8/21 10:42
 */
public class HistoryHandler extends Handler<ShellContext> {
    @Override
    public ShellContext handler(ShellContext result) {
        String msg = result.getMsg();
        log.info("接收到消息:{}", msg);
        System.out.println(msg);
        UserSessionInfo sessionInfo = SessionContext.get();
        String historyInsertStr = SshCore.getBeforStr(sessionInfo.getUserId(), sessionInfo.getSessionKey());
        if(!msg.startsWith("\n") && StringUtils.isNotEmpty(historyInsertStr)){
            msg = historyInsertStr + msg;
            result.setMsg(msg);
            return result;
        }
        return result;
    }

    @Override
    public boolean accept(ShellContext command) {
        return !command.getMsg().startsWith("\n");
    }
}
